/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.redot.repository;

import com.yunqian.redot.domain.TicketOrder;
import com.yunqian.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 客户门票订单记录
 *
 * @author zsy
 * 2018-09-29 15:55:07
 */
@Repository
public interface TicketOrderRepository extends BaseRepository<TicketOrder, Long> {

    /**
     * 统计即将失效的订单门票数 基础票
     * @return
     */
    @Query(value = "select ticket_id as ticketId,ifnull(sum(quantity),0) as quantity from t_ticket_order where status = 1 and now() > date_add(save_time, interval 30 minute) and ticket_id in (select id from t_ticket where type = 1 and status = 1) group by ticket_id", nativeQuery =  true)
    List<Map<String,Object>> findAllByOrderStatus();

    /**
     * 将30分钟内未付款的订单状态改为3（取消）
     */
    @Modifying
    @Query(value = "update t_ticket_order set status = 3 where status = 1 and now() > date_add(save_time, interval 30 minute)", nativeQuery = true)
    void updateOrderStatus();

    /**
     * 根据订单号查询
     * @param orderNo
     * @return
     */
    TicketOrder findByOrderNo(String orderNo);
}
